package bookManageSystem.view;

import bookManageSystem.Mat.StaticMapExample;
import bookManageSystem.po.Admin;
import bookManageSystem.po.Employee;
import bookManageSystem.service.AdminsService;
import bookManageSystem.service.EmployeeService;
import bookManageSystem.tools.ComponentTools;
import bookManageSystem.tools.SimpleTools;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LogupFrame extends JFrame implements ActionListener {
    private EmployeeService employeeService = new EmployeeService(); // 员工服务实例
    private AdminsService adminsService = new AdminsService(); // 员工服务实例
    private SimpleTools simpleTools = new SimpleTools();
    private ComponentTools componentTools = new ComponentTools();
    private JPanel panel;
    private Box totalVBox, systemLabelBox, usernameBox, passwordBox, userTypeBox, buttonBox;
    private JLabel systemLabel, usernameLabel, passwordLabel, userTypeLabel;
    private JTextField usernameTextField;
    private JPasswordField passwordField;
    private JComboBox<String> userTypeComboBox;
    private JButton logupButton, resetButton;

    public LogupFrame() {
        this.setTitle("登录");
        this.setBounds(400, 400, 600, 500);

        this.setContentPane(this.createLogupPane());

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(false);

        // 为按钮注册事件
        logupButton.addActionListener(this);
        resetButton.addActionListener(this);
    }

    public JPanel createLogupPane() {
        panel = new JPanel();
        totalVBox = Box.createVerticalBox();

        systemLabelBox = Box.createHorizontalBox();
        systemLabel = new JLabel("医疗信息管理系统");
        systemLabel.setFont(new Font("微软雅黑", Font.BOLD, 40));
        systemLabelBox.add(systemLabel);

        usernameBox = Box.createHorizontalBox();
        usernameLabel = new JLabel("用户名：");
        usernameTextField = new JTextField(20);
        usernameBox.add(usernameLabel);
        usernameBox.add(Box.createHorizontalStrut(20));
        usernameBox.add(usernameTextField);

        passwordBox = Box.createHorizontalBox();
        passwordLabel = new JLabel("密    码：");
        passwordField = new JPasswordField(20);
        passwordBox.add(passwordLabel);
        passwordBox.add(Box.createHorizontalStrut(20));
        passwordBox.add(passwordField);

        userTypeBox = Box.createHorizontalBox();
        userTypeLabel = new JLabel("用户类型：");
        userTypeComboBox = new JComboBox<>(new String[]{"用户", "管理员"});
        userTypeBox.add(userTypeLabel);
        userTypeBox.add(Box.createHorizontalStrut(20));
        userTypeBox.add(userTypeComboBox);

        buttonBox = Box.createHorizontalBox();
        logupButton = new JButton("登录");
        resetButton = new JButton("重置");
        buttonBox.add(logupButton);
        buttonBox.add(Box.createHorizontalStrut(80));
        buttonBox.add(resetButton);

        totalVBox.add(systemLabelBox);
        totalVBox.add(Box.createVerticalStrut(80));
        totalVBox.add(usernameBox);
        totalVBox.add(Box.createVerticalStrut(40));
        totalVBox.add(passwordBox);
        totalVBox.add(Box.createVerticalStrut(40));
        totalVBox.add(userTypeBox);
        totalVBox.add(Box.createVerticalStrut(40));
        totalVBox.add(buttonBox);

        panel.add(totalVBox);

        // 为标签设置图标
        new ComponentTools().setIcons(new JLabel[]{systemLabel, usernameLabel, passwordLabel, userTypeLabel}, new String[]{"src/bookManageSystem/images/logo.png", "src/bookManageSystem/images/userName.png", "src/bookManageSystem/images/password.png", "src/bookManageSystem/images/userType.png"});
        // 为按钮设置图标
        new ComponentTools().setIcons(new JButton[]{logupButton, resetButton}, new String[]{"src/bookManageSystem/images/login.png", "src/bookManageSystem/images/reset.png"});
        return panel;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == logupButton) {
            if (!simpleTools.isEmpty(usernameTextField.getText()) || !simpleTools.isEmpty(passwordField.getText())) {
                JOptionPane.showMessageDialog(null, "用户名和密码不能为空！");
            } else {
                String username = usernameTextField.getText();
                String password = new String(passwordField.getPassword());
                String userType = (String) userTypeComboBox.getSelectedItem();
                StaticMapExample.put("name",username);
                StaticMapExample.put("userType",userType);
//                区分用户和管理员 姓名是 admin 密码是123 就是管理员

                if ("管理员".equals(userType)) {
              Admin admin =       adminsService.getAdminByName(username);
                    if (username.equals(admin.getAdminName()) && password.equals(admin.getPassword())) {
                        int isOK = JOptionPane.showConfirmDialog(null, "恭喜您，登录成功！");
                        if (isOK == JOptionPane.OK_OPTION) {
                            StaticMapExample.put("password",password);
                            componentTools.reset(usernameTextField, passwordField);
                            new MainFrame(userType).setVisible(true); // 假设MainFrame是主界面
                            this.setVisible(false);
                        }
                    } else {
                        JOptionPane.showMessageDialog(null, "对不起，登录失败！");
                    }

                } else {
                    Employee employee = employeeService.getEmployeeByUsername(username); // 假设有此方法
                    if (employee != null && password.equals(employee.getPassword())) {
                        int isOK = JOptionPane.showConfirmDialog(null, "恭喜您，登录成功！");
                        if (isOK == JOptionPane.OK_OPTION) {
                            StaticMapExample.put("userId",employee.getUserId()+"");
                            StaticMapExample.put("phone",employee.getPhone());
                            StaticMapExample.put("balance",employee.getBalance()+"");
                            StaticMapExample.put("userType",userType);
                            StaticMapExample.put("password",password);
                            componentTools.reset(usernameTextField, passwordField);
                            new MainFrame(userType).setVisible(true); // 假设MainFrame是主界面
                            this.setVisible(false);
                        }

                    } else {
                        JOptionPane.showMessageDialog(null, "对不起，登录失败！");
                    }


                    if (e.getSource() == resetButton) {
                        componentTools.reset(usernameTextField, passwordField);
                        userTypeComboBox.setSelectedIndex(0); // 重置为默认选项
                    }
                }
            }
        }
    }
}